package yaml

import (
	"errors"
	"gopkg.in/yaml.v3"
)

type Person struct {
	Name    string `yaml:"name"`
	Age     int    `yaml:"age"`
	Address string `yaml:"address"`
}

func ToYamlString(data interface{}) (string, error) {
	yamlStr, err := yaml.Marshal(data)
	if err != nil {
		return "", err
	}
	return string(yamlStr), nil
}

func GetObject(yamlStr string) (Person, error) {
	var p Person
	if len(yamlStr) == 0 {
		return p, errors.New("yamlStr is null")
	}
	err := yaml.Unmarshal([]byte(yamlStr), &p)
	if err != nil {
		return p, err
	}
	return p, nil
}
